package com.example.gateway.web;


import com.example.gateway.event.RefreshRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.web.ZuulHandlerMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Created by xujingfeng on 2017/4/1.
 */
@RestController
public class DemoController {

    @Autowired
    RefreshRouteService refreshRouteService;
    @Autowired
    ZuulHandlerMapping zuulHandlerMapping;

    /**
     * 刷新网关
     * @return
     */
    @RequestMapping("/refreshRoute")
    public String refreshRoute(){
        refreshRouteService.refreshRoute();
        return "refreshRoute";
    }

    /**
     * 查看网关规则
     * @return
     */
    @RequestMapping("/watchNowRoute")
    public String watchNowRoute(){
        //可以用debug模式看里面具体是什么
        Map<String, Object> handlerMap = zuulHandlerMapping.getHandlerMap();
        Set<Map.Entry<String,Object>> st = handlerMap.entrySet();
        Iterator<Map.Entry<String,Object>> it = st.iterator();
        while (it.hasNext()){
            Map.Entry<String,Object> en = it.next();
            System.out.println("规则： " + en.getKey() + ",  "+ en.getValue().toString());
        }
        return "watchNowRoute";
    }



}
